package com.wn.cainiao.widget

import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.wn.cainiao.utils.ReFragment

/**
 * @Author: wn
 * @Date: 2023/3/27 17:06
 * @Description: ViewPager2 的 Adapter
 */
class MainViewPagerAdapter(
  fragmentActivity: FragmentActivity,
  private val fragments: Map<Int, ReFragment>
) : FragmentStateAdapter(fragmentActivity) {
  // 有多少个元素
  override fun getItemCount() = fragments.size

  // 创建fragment invoke回调函数，lambda 调用 实例化 Fragment
  override fun createFragment(position: Int) =
    fragments[position]?.invoke() ?: error("请确保fragment数据源和viewPager2的index匹配设置")
}
